package cn.dglydrpy.study.j2ee.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

import org.bson.Document;

public class DocumentTest {
	
    public static void main(String[] args) {
    	
    	MongoClient  mongoClient  = new MongoClient("192.168.80.131",27017);
        // 获取数据库对象
        MongoDatabase  mongoDatabase  = mongoClient.getDatabase("study");
        // 获取集合对象
        MongoCollection<Document>  collection = mongoDatabase.getCollection("study_preview");
        
        //  构建Document 对象  并插入到集合中
        Document  document  = Document.parse("{name:'lisi',city:'北京',birth_day:new ISODate('2001-08-01'),expectSalary:18000}");
        collection.insertOne(document);
        
        
        // 查询: 根据expectSalary 降序排列
        Document  sortDocument  =  new Document();
        sortDocument.append("expectSalary",-1);
        //2种不同的写法都可以
        //FindIterable<Document>  findIterable =  collection.find(Document.parse("{expectSalary:{$gt:21000}}")).sort(sortDocument);
        FindIterable<Document>  findIterable =  collection.find(Filters.gt("expectSalary",21000)).sort(sortDocument);
        for(Document doc : findIterable){
            System.out.println(doc);
        }
        
        mongoClient.close();
    }
}
